//package com.example.demo1.util;
//
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//
//public class PswdUtil {
//    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//
//    // 加密密码
//    public static String encode(String rawPassword) {
//        return encoder.encode(rawPassword);
//    }
//
//    // 验证密码
//    public static boolean matches(String rawPassword, String encodedPassword) {
//        return encoder.matches(rawPassword, encodedPassword);
//    }
//
//
//}
//
package com.example.demo1.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;

public class PswdUtil {

    // 加密密码（MD5 实现）
    public static String encode(String rawPassword) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(rawPassword.getBytes(StandardCharsets.UTF_8));

            StringBuilder hexBuilder = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xFF & b);
                if (hex.length() == 1) hexBuilder.append('0'); // 补零保证两位
                hexBuilder.append(hex);
            }
            return hexBuilder.toString();

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 算法不存在", e);
        }
    }

    // 验证密码（MD5 实现）
    public static boolean matches(String rawPassword, String encodedPassword) {
        String encoded = encode(rawPassword);
        return encoded.equals(encodedPassword);
    }
}